<?php

namespace app\rent\controller;

use think\Controller;

class Business extends Controller
{

  public function _initialize()
  {
    parent::_initialize();
    // 实例化模型   
    $this->BusinessModel = model('Business.Business');
    $this->LeaseModel = model('Lease');

  }

  // 注册
  public function register()
  {
    $mobile = $this->request->param('mobile', '', 'trim');
    $password = $this->request->param('password', '', 'trim');

    $salt = randstr();

    // 密码加密
    $password = md5($password . $salt);

    $data = [
      'mobile' => $mobile,
      'password' => $password,
      'salt' => $salt,
      'nickname' => $mobile
    ];

    $result = $this->BusinessModel->validate('common/Business/Business')->save($data);

    if ($result) {
      $this->success('注册成功');
    } else {
      $this->error($this->BusinessModel->getError());
    }
  }

  //登陆
  public function login()
  {
    $mobile = $this->request->param('mobile', '', 'trim');
    $password = $this->request->param('password', '', 'trim');

    // 查询单条数据 1 代表主键
    $Businessinfo = $this->BusinessModel->where(['mobile' => $mobile])->find();

    if (!$Businessinfo) {
      $this->error('当前用户不存在');
    }

    // 密码加密
    $password = md5($password . $Businessinfo['salt']);
    // echo '<script>console.log('.$password.')</script>';
    // halt($password);
    if ($password != $Businessinfo['password']) {
      $this->error('帐号密码错误');
    }

    $data = [
      'id' => $Businessinfo['id'],
      'mobile' => $Businessinfo['mobile'],
      'avatar' => $Businessinfo['avatar_text'],
      'auth' => $Businessinfo['auth'],
      'email' => $Businessinfo['email'],
      'gender' => $Businessinfo['gender'],
      'money' => $Businessinfo['money'],
      'nickname' => $Businessinfo['nickname'],
      'province' => $Businessinfo['province'],
      'city' => $Businessinfo['city'],
      'district' => $Businessinfo['district'],
      'createtime' => $Businessinfo['createtime'],
      'region_text' => $Businessinfo['region_text'],
    ];

    $this->success('登陆成功', '', $data);
  }


  // 校验
  public function check()
  {
    $mobile = $this->request->param('mobile', '', 'trim');
    $id = $this->request->param('id', 0, 'trim');

    // 查询单条数据 1 代表主键
    $Businessinfo = $this->BusinessModel->where(['mobile' => $mobile, 'id' => $id])->find();

    if (!$Businessinfo) {
      $this->error('当前用户不存在');
    }

    $data = [
      'id' => $Businessinfo['id'],
      'mobile' => $Businessinfo['mobile'],
      'avatar' => $Businessinfo['avatar_text'],
      'auth' => $Businessinfo['auth'],
      'email' => $Businessinfo['email'],
      'gender' => $Businessinfo['gender'],
      'money' => $Businessinfo['money'],
      'nickname' => $Businessinfo['nickname'],
      'province' => $Businessinfo['province'],
      'city' => $Businessinfo['city'],
      'district' => $Businessinfo['district'],
      'createtime' => $Businessinfo['createtime'],
      'region_text' => $Businessinfo['region_text'],
    ];

    $this->success('登陆成功', '', $data);
  }


  //用户信息
  public function profile()
  {
    $id = $this->request->param('id', 0, 'trim');
    $params = $this->request->param();
    $password = $this->request->param('password', '', 'trim');

    // 查询单条数据 1 代表主键
    $Businessinfo = $this->BusinessModel->find($id);

    if (!$Businessinfo) {
      $this->error('当前用户不存在');
    }

    $data = [
      'id' => $id,
      'nickname' => $params['nickname'],
      'email' => $params['email'],
      'gender' => $params['gender'],
      'province' => $params['province'],
      'city' => $params['city'],
      'district' => $params['district'],
    ];

    // 是否修改密码
    if ($password) {
      $salt = randstr();
      // 密码加密
      $password = md5($password . $salt);
      $data['password'] = $password;
      $data['salt'] = $salt;
    }

    // 是否有修改头像
    if (isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0) {
      $res = upload('avatar');
      if ($res['code'] == 0) {
        $this->error($res['msg']);
      }
      $data['avatar'] = $res['data'];
    }

    $result = $this->BusinessModel->isUpdate()->save($data);

    if ($result === false) {
      if (isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0) {
        is_file(trim($data['avatar'], '/')) && unlink(trim($data['avatar'], '/'));
      }
      $this->error('修改资料失败');
    } else {
      if (isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0) {
        is_file(trim($Businessinfo['avatar'], '/')) && unlink(trim($Businessinfo['avatar'], '/'));
      }

      $info = $this->BusinessModel->find($id);

      $datas = [
        'id' => $info['id'],
        'mobile' => $info['mobile'],
        'avatar' => $info['avatar_text'],
        'auth' => $info['auth'],
        'email' => $info['email'],
        'gender' => $info['gender'],
        'money' => $info['money'],
        'nickname' => $info['nickname'],
        'province' => $info['province'],
        'city' => $info['city'],
        'district' => $info['district'],
        'createtime' => $info['createtime'],
        'region_text' => $info['region_text'],
      ];
      $this->success('修改成功', '', $datas);
    }
  }

  //消费记录
  public function leaserecords()
  {
    $busid = $this->request->param('busid', 0, 'trim');

    $Businessinfo = $this->BusinessModel->find($busid);
    if (!$Businessinfo) {
      $this->error('用户不存在');
    }

    $receive = $this->LeaseModel->where(['busid' => $busid])->select();
    if (count($receive) > 0) {
      $this->success('查询成功', '', $receive);
    } else {
      $this->error('暂无数据');
    }
  }
}
